package com.xinzhi.utils;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.xinzhi.model.Alipay;
import com.xinzhi.model.AlipayBean;

/* 支付宝 */
public class AlipayUtil {
    //连接
    public static String connect(AlipayBean alipayBean) throws AlipayApiException {
        //1、获得初始化的AlipayClient 获取商户的属性 找到商户
        Alipay alipay = new Alipay();
        AlipayClient alipayClient = new DefaultAlipayClient(
                alipay.getGatewayUrl(),
                alipay.getApp_id(),
                alipay.getMerchant_private_key(),
                "json",
                alipay.getCharset(),
                alipay.getAlipay_public_key(),
                alipay.getSign_type()
        );

        //2、设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        //页面跳转同步通知页面路径
        alipayRequest.setReturnUrl(alipay.getReturn_url());
        // 服务器异步通知页面路径
        alipayRequest.setNotifyUrl(alipay.getNotify_url());
        //封装参数
        alipayRequest.setBizContent(JSON.toJSONString(alipayBean));

        //3、请求支付宝进行付款，并获取支付结果
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //返回付款信息
        return result;
    }
}
